<template>
  <div>

    <div class="login">
      <div class="center">
        <!-- <h1><img src="./imgs/Windows10.png" alt=""></h1> -->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
          label-position="left">
          <div class="inputLi">
            <el-input ref="email" class="user" v-model="loginForm.email" prefix-icon="el-icon-user" placeholder="请输入邮箱"
              name="email" type="text" tabindex="1" autocomplete="on" />
            <!-- <input ref="email" type="text" class="user" placeholder="请输入" v-model="loginForm.email" required> -->
          </div>
          <div class="inputLi">
            <input ref="password" type="password" class="pwd" placeholder="请输入" v-model="loginForm.password" required
              @keyup.enter.native="handleLogin">
          </div>
          <div class="line-two">
            <slide-verify :l="slideObj.l" :r="slideObj.r" :w="slideObj.w" :h="slideObj.h" slider-text="请拖动滑块完成验证再确认领取"
              @success="onSuccess" @fail="onFail" @refresh="onRefresh" :imgs="imgs" :accuracy='2'></slide-verify>
            <!-- accuracy容错率 -->
          </div>
          <div style="width:400px">
            <!-- <button @click.native.prevent="handleLogin">登录</button> -->
           <el-button disabled type="primary" :disabled="statusa" @click.native.prevent="handleLogin" style="width: 360px;height: 40px;margin: 20px;" ><i
                class="el-icon-s-promotion" /> 登 录</el-button>
                <!-- <el-button type="primary" plain disabled>主要按钮</el-button> -->
          </div>
        </el-form>
        <!-- <p class="tip">关注17素材网</p> -->
      </div>
    </div>

  </div>
</template>

<script>
  // import { validUsername } from '@/utils/validate'

  export default {
    name: 'Login',
    data() {
      return {
        statusa:true,
        slideObj: {
          l: 26, //滑动碎片的大小
          r: 14, //滑动碎片的圆角
          w: 210, //画布的宽
          h: 155, //画布的高
        },
        demo: false,
        imgs: [
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fitbbs%2F1411%2F03%2Fc11%2F40513136_1415006215595.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658045243&t=66cafeed4b05e3cc2337bd619564552a",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0146425bbdc6efa801213dead7d60e.jpg%401280w_1l_2o_100sh.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658045243&t=e11d669624d5ac428996e933f67f5d93",
         // "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201804%2F28%2F20180428190056_cnorp.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658045243&t=a174254b82136a546d08f4e903dddb59",
          "https://gss0.baidu.com/70cFfyinKgQFm2e88IuM_a/baike/pic/item/14ce36d3d539b6005fd3d015e550352ac65cb777.jpg",
          "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbkimg.cdn.bcebos.com%2Fpic%2F18d8bc3eb13533fa828b4f630598ea1f4134970aed60&refer=http%3A%2F%2Fbkimg.cdn.bcebos.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658045243&t=fb598fe1de3576cf76269f913d9fad0b",
        ], //可以将所需的图片放在一个数组中，刷新后组件会自动随机抽取图片


        loginForm: {
          email: '',
          password: ''
        },
        loginRules: {
          email: [{
            required: true,
            type: 'email',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            trigger: 'blur'
          }]
        },
        passwordType: 'password',
        capsTooltip: false,
        loading: false,
        showDialog: false,
        redirect: undefined,
        otherQuery: {}
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          const query = route.query
          if (query) {
            this.redirect = query.redirect
            this.otherQuery = this.getOtherQuery(query)
          }
        },
        immediate: true
      }
    },
    created() {
      // window.addEventListener('storage', this.afterQRScan)
    },
    mounted() {
      if (this.loginForm.email === '') {
        this.$refs.email.focus()
      } else if (this.loginForm.password === '') {
        this.$refs.password.focus()
      }

      let clientWidth = document.documentElement.clientWidth; //获取HTML文档所在窗口的当前宽度
      let ratio = (clientWidth / 375) * 7; //计算基数为40的比例,然后动态计算画布在当前窗口的宽高，撑开弹窗
      this.slideObj.l = 1.2 * ratio;
      this.slideObj.r = 0.3 * ratio;
      this.slideObj.w = 11 * ratio;
      this.slideObj.h = 4 * ratio;

    },
    destroyed() {
      // window.removeEventListener('storage', this.afterQRScan)
    },
    methods: {
      onSuccess() {

        this.statusa = false
        console.log("成功回调");

      },
      onFail() {
        // console.log("失败回调");
         this.onRefresh();
      },
      onRefresh() {
        this.msg = "再试一次";
        // console.log("刷新回调");
      },

      checkCapslock(e) {
        const {
          key
        } = e
        this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                this.$router.push({
                  path: this.redirect || '/',
                  query: this.otherQuery
                })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      getOtherQuery(query) {
        return Object.keys(query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
      }
    }
  }
</script>
<style>
  .login {
    background-image: linear-gradient(-20deg, #b721ff 0%, #21d4fd 100%);
    height: 100vh;
    width: 100vw;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
  }

  .login .center {
    background: #fff;
    padding: 25px;
    min-width: 370px;
    -webkit-box-shadow: 0 0 5px #333;
    box-shadow: 0 0 5px #333;
  }

  .login .center h1 {
    color: #66a6ff;
    font-weight: bold;
    padding-bottom: 15px;
    text-align: center;
    -webkit-animation: moveTopBottom 2s linear forwards;
    animation: moveTopBottom 2s linear forwards;
    font-family: Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
  }

  .login .center .inputLi {
    margin-bottom: 15px;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
  }

  .login .center .inputLi input {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    border: 1px solid #ddd;
    height: 45px;
    padding: 0 10px 0 40px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #66a6ff;
    font-size: 18px;
    outline: none;
  }

  .login .center .inputLi input::-webkit-input-placeholder {
    color: #999;
    font-size: 12px;
  }

  .login .center .inputLi input:-ms-input-placeholder {
    color: #999;
    font-size: 12px;
  }

  .login .center .inputLi input::-ms-input-placeholder {
    color: #999;
    font-size: 12px;
  }

  .login .center .inputLi input::placeholder {
    color: #999;
    font-size: 12px;
  }

  .login .center .inputLi input:focus {
    border: 1px solid #66a6ff;
  }

  .login .center .inputLi .user {
    background: #fff url(./imgs/user.png) left 10px center no-repeat;
    background-size: 20px 20px;
  }

  .login .center .inputLi .pwd {
    background: #fff url(./imgs/pwd.png) left 10px center no-repeat;
    background-size: 20px 20px;
  }

  .login .center .inputLi button {
    background-image: -webkit-gradient(linear, left bottom, left top, from(#09203f), to(#537895));
    background-image: linear-gradient(to top, #09203f 0%, #537895 100%);
    border: none;
    height: 45px;
    color: #fff;
    outline: none;
    font-weight: bold;
    letter-spacing: 3px;
    font-family: "微软雅黑";
    font-size: 18px;
    cursor: pointer;
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
  }

  .login .center .inputLi button:active {
    opacity: 0.8;
  }

  .login .center .tip {
    font-size: 12px;
    color: #999;
  }

  @-webkit-keyframes moveTopBottom {
    0% {
      -webkit-filter: grayscale(100%);
      filter: grayscale(100%);
    }

    20% {
      -webkit-filter: grayscale(80%);
      filter: grayscale(80%);
    }

    40% {
      -webkit-filter: grayscale(60%);
      filter: grayscale(60%);
    }

    60% {
      -webkit-filter: grayscale(40%);
      filter: grayscale(40%);
    }

    80% {
      -webkit-filter: grayscale(20%);
      filter: grayscale(20%);
    }

    100% {
      -webkit-filter: grayscale(0%);
      filter: grayscale(0%);
    }
  }

  @keyframes moveTopBottom {
    0% {
      -webkit-filter: grayscale(100%);
      filter: grayscale(100%);
    }

    20% {
      -webkit-filter: grayscale(80%);
      filter: grayscale(80%);
    }

    40% {
      -webkit-filter: grayscale(60%);
      filter: grayscale(60%);
    }

    60% {
      -webkit-filter: grayscale(40%);
      filter: grayscale(40%);
    }

    80% {
      -webkit-filter: grayscale(20%);
      filter: grayscale(20%);
    }

    100% {
      -webkit-filter: grayscale(0%);
      filter: grayscale(0%);
    }
  }
</style>
